﻿using System;
using System.Collections.Specialized;
using System.Web;
using System.Web.Security;
using Arch;
using Pharmacy.Data;
using Pharmacy.Data.SqlClient;
using Pharmacy.Entities;

public partial class Admin_Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            var httpCookie = Request.Cookies["LoginUser"];
            var conCookie = Request.Cookies["ConnectionString"];
            if (httpCookie != null && conCookie != null)
                FormsAuthentication.RedirectFromLoginPage(httpCookie.Value, false);
        }
    }
    protected void btn_Submit_Click(object sender, EventArgs e)
    {
        //Request.Cookies["LoginUser"] = new HttpCookie("LoginUser","vy.le");
        string whereExpression =String.Format("UserId='{0}' And Password='{1}'",tbl_Username.Text.Replace('\'',' '),tbl_Password.Text.Replace('\'',' '));
        int count;
        SqlNetTiersProvider provider = GlobalUtilities.CreateProvider(Server.MapPath("~") + "/Config.xml", ddl_Server.SelectedValue);
        DataRepository.LoadProvider(provider, true);
        TList<WebUser> webUsers = DataRepository.WebUserProvider.GetPaged(whereExpression, "", 0, 1, out count);
        if (count > 0)
        {
            HttpCookie cookie = new HttpCookie("LoginUser",webUsers[0].UserId );
            HttpCookie con = new HttpCookie("ConnectionString", ddl_Server.SelectedValue);
            cookie.Expires = DateTime.Now.AddDays(1);
            con.Expires = DateTime.Now.AddDays(1);
            Response.Cookies.Add(cookie);
            Response.Cookies.Add(con);
            FormsAuthentication.RedirectFromLoginPage(cookie.Value, true);
        }
        else
        {
            div_Error.Visible = true;
        }
    }
}